package sguigu.threee.test;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/12/1 10:36
 * @Version: 1.0
 */
public class LRUCacheDemo{

    //双向链表节点
    static class Node<K,V>{
        K key;
        V value;
        Node<K, V> prev;
        Node<K, V> next;

        public Node() {
            this.prev = this.next = null;
        }
        public Node(K key, V value) {
            super();
            this.key = key;
            this.value = value;
        }

    }

    //构建一个虚拟的双向链表，里面安放的就是我们的Node
    //新的插入头部，旧的从尾部移除
    class DoubleLinkedList<K,V>{

        Node<K,V> head; //头
        Node<K,V> tail; //尾

        //构造方法
        public DoubleLinkedList(){
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.prev = head;
        }

        //添加头部
        public void addHead(Node<K,V> node){
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;
        }

        //删除结点
        public void removeNode(Node<K,V> node){
            node.next.prev = node.prev;
            node.prev.next = node.next;
            node.prev = null;
            node.next = null;

        }

        //获取最后一个节点
        public Node<K,V> getLast(){
            return tail.prev;
        }

    }

    private int cacheSize;
    private Map<Integer, Node<Integer, Integer>> map;
    private DoubleLinkedList<Integer, Integer> doubleLinkedList;

    public LRUCacheDemo(int cacheSize){
        this.cacheSize = cacheSize; //坑位
        map = new HashMap<>(); //查找
        doubleLinkedList = new DoubleLinkedList<>();
    }

    public int get(int key){
        if(!map.containsKey(key)){
            return -1;
        }
        //更新节点位置，将节点移置链表头
        Node<Integer, Integer> node = map.get(key);
        doubleLinkedList.removeNode(node);
        doubleLinkedList.addHead(node);
        return node.value;
    }

    //saveOrUpdate
    public void put(int key,int value){
        if(map.containsKey(key)){ //更新
            Node<Integer, Integer> node = map.get(key);
            //更新map
            node.value = value;
            map.put(key,node);
            //更新链表
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        }else {
            if(map.size() == cacheSize){ //坑位满了
                Node<Integer,Integer> lastNode = doubleLinkedList.getLast();
                map.remove(lastNode.key);
                doubleLinkedList.removeNode(lastNode);
            }
            //save
            Node<Integer, Integer> newNode = new Node<>(key, value);
            map.put(key,newNode);
            doubleLinkedList.addHead(newNode);
        }
    }




    public static void main(String[] args) {

        LRUCacheDemo lruCache = new LRUCacheDemo(3);
        lruCache.put(1,1);
        lruCache.put(2,2);
        lruCache.put(3,3);

        System.out.println(lruCache.map.keySet());

        lruCache.put(4,4);

        System.out.println(lruCache.map.keySet());
        System.out.println();
        lruCache.put(3,3);
        System.out.println(lruCache.map.keySet());
        lruCache.put(3,3);
        System.out.println(lruCache.map.keySet());
        lruCache.put(3,3);
        System.out.println(lruCache.map.keySet());
        System.out.println();
        lruCache.put(5,5);
        System.out.println(lruCache.map.keySet());


    }
}
